package jaeger

import (
	"os"
	"time"
)

var CLI *jaeger

type jaeger struct {
	Url      string
	Lookback int64
}

func (r *jaeger) DrawGlueLink(guid string, containers []string) ([]map[string]interface{}, error) {
	var data = make(map[string]interface{})
	if err := jaegerTracesGluenets(r.Url, guid, containers, r.Lookback, data); err != nil {
		return nil, err
	}
	if len(data) == 0 {
		return nil, nil
	}
	link, err := drawLinksGlue(data)
	if err != nil {
		return nil, err
	}
	return link, nil
}

func (r *jaeger) DrawK8sLink(guid string) ([]map[string]interface{}, error) {
	var data = make(map[string]interface{})
	if err := jaegerTracesK8s(r.Url, guid, r.Lookback, data); err != nil {
		return nil, err
	}
	if len(data) == 0 {
		return nil, nil
	}
	link, err := drawLinksK8s(data)
	if err != nil {
		return nil, err
	}

	return link, nil
}

func Install() error {
	url := "39.101.140.145:30025"

	mode := os.Getenv("MODE")
	if mode == "prod" {
		url = os.Getenv("JAEGER_URL")
	}

	CLI = &jaeger{
		Url:      url,
		Lookback: int64(time.Hour.Seconds()) * 24 * 10,
	}
	return nil
}
